package com.zhifa.hdtyg_cdgl.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.zhifa.hdtyg_cdgl.dao.SiteUserTestMapper;
import com.zhifa.hdtyg_cdgl.entity.SiteInformation;
import com.zhifa.hdtyg_cdgl.entity.SiteReservation;
import com.zhifa.hdtyg_cdgl.service.SiteInformationService;
import com.zhifa.hdtyg_cdgl.service.SiteReservationService;
import com.zhifa.hdtyg_cdgl.service.SiteUserTestService;
import com.zhifa.hdtyg_cdgl.util.ComboboxUtil;
import com.zhifa.hdtyg_cdgl.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.util.DateUtils;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/siteReservation")
public class SiteReservationController {

    // 自定义类型转换器
    @InitBinder
    public void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {

       // System.out.println("initBinder  &&&&"+request.getParameter("hiredate")+"***"+request.getParameter("username"));

        binder.registerCustomEditor(Date.class,
                new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
    }

    @Autowired
    SiteReservationService siteReservationService;

    @Autowired
    SiteInformationService siteInformationService;

    @Autowired
    SiteUserTestMapper siteUserTestMapper;

    @Autowired
    SiteUserTestService siteUserTestService;


    @RequestMapping("/addSiteReservation")
    public Map<String,Object> addSiteReservation(Integer siteId,String reason,String applicationTime, String whenTime,HttpSession httpSession) throws Exception {
        SiteReservation siteReservation =new SiteReservation();

        Integer user_id = (Integer) httpSession.getAttribute("user_id");
        siteReservation.setUserId(user_id);
        siteReservation.setSiteId(siteId);
        siteReservation.setApplicationTime(DateUtil.formatString(applicationTime,"yyyy-MM-dd"));
        siteReservation.setWhenTime(whenTime);
        siteReservation.setReason(reason);
        //siteReservation.setReturnTime(DateUtil.formatString(returnTime,"yyyy-MM-dd HH:mm:ss"));//DateUtil.formatString(returnTime,"yyyy-MM-dd HH:mm:ss")
        //siteReservation.setApplicationTime(new Date());
        siteReservation.setStatus("正在审核");
        SiteReservation tempSiteReservation = siteReservationService.findSiteReservation(siteReservation);
        Map<String,Object> map=new HashMap<>();
        if(tempSiteReservation!=null){
            map.put("success",false);
            map.put("msg", "重复预约");
            return  map;
        }
        int i = siteReservationService.addSiteReservation(siteReservation);
        if (i>0){
            map.put("success",true);
        }else {
            map.put("success",false);
            map.put("msg", "");
        }
        return map;

    }
    @RequestMapping("/getWhenTime")
    public JSONArray getWhenTime(Integer siteId,String applicationTime) throws Exception {
        Date date=DateUtil.formatString(applicationTime,"yyyy-MM-dd");
        List<SiteReservation> whemTime = siteReservationService.findWhemTime(siteId, date);
        JSONArray jsonArray = new JSONArray();
        if (whemTime.size()!=0){
            jsonArray = ComboboxUtil.getSomeWhentime(whemTime);
        }else {
            jsonArray=ComboboxUtil.getAllWhenTime();

        }
        return  jsonArray;
    }

    @RequestMapping("/findMySiteReservation")
    public JSONObject findMySiteReservation(SiteReservation siteReservation, int page, int rows,HttpSession httpSession){
        Integer user_id = (Integer) httpSession.getAttribute("user_id");
        siteReservation.setUserId(user_id);
        PageInfo<SiteReservation> mySiteReservation = siteReservationService.findMySiteReservation(siteReservation, page, rows);
        List<SiteReservation> list = mySiteReservation.getList();
        JSONArray jsonArray=new JSONArray();
        for (int i = 0; i < list.size(); i++) {

            SiteReservation siteReservation1 = list.get(i);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("userId", user_id);
            jsonObject.put("siteYyId",siteReservation1.getSiteYyId());
            jsonObject.put("applicationTime", siteReservation1.getApplicationTime());
            jsonObject.put("whenTime", siteReservation1.getWhenTime());
            jsonObject.put("status", siteReservation1.getStatus());

            Integer siteId = siteReservation1.getSiteId();
            jsonObject.put("siteId",siteId);
            SiteInformation siteInformationBySiteId = siteInformationService.findSiteInformationBySiteId(siteId);
            jsonObject.put("name", siteInformationBySiteId.getName());
            jsonObject.put("position",siteInformationBySiteId.getPosition());
            jsonObject.put("price", siteInformationBySiteId.getPrice());
            jsonArray.add(jsonObject);

        }
        JSONObject jsonObject = new JSONObject();
        long total = mySiteReservation.getTotal();
        jsonObject.put("total", total);
        jsonObject.put("rows",jsonArray);


        return jsonObject;
    }

    @RequestMapping("/findStatus")
    public JSONArray findStatus(){
        List<String> status = siteReservationService.findStatus();
        JSONArray jsonArray = ComboboxUtil.changeListToJsonarray(status);
        return jsonArray;
    }

    @RequestMapping("/changeMyYuyueSatus")
    public Map<String,Object> changeMyYuyueSatus(String applicationTime,String whenTime,Integer userId,Integer siteId) throws Exception {
        SiteReservation siteReservation=new SiteReservation();
        siteReservation.setApplicationTime(DateUtil.formatString(applicationTime,"yyyy-MM-dd"));
        siteReservation.setWhenTime(whenTime);
        siteReservation.setUserId(userId);
        siteReservation.setSiteId(siteId);
        /*applicationTime:row.applicationTime,whenTime:row.whenTime,userId:row.userId,siteId:row.siteId*/
        siteReservation.setStatus("取消预约");
        int i = siteReservationService.changeMyYuyueSatus(siteReservation);
        Map<String,Object> map=new HashMap<>();
        if (i>0){
            map.put("success",true);
        }else {
            map.put("success",false);
        }
        return map;
    }

    @RequestMapping("/findAllSiteReservation")
    public JSONObject findAllSiteReservation(SiteReservation siteReservation, int page, int rows,HttpSession httpSession) throws Exception {
        //siteReservation.setApplicationTime(DateUtil.formatString(applicationTime,"yyyy-MM-dd"));
        //Integer user_id = (Integer) httpSession.getAttribute("user_id");
        PageInfo<SiteReservation> allSiteReservation = siteReservationService.findAllSiteReservation(siteReservation, page, rows);
        List<SiteReservation> list = allSiteReservation.getList();

        JSONArray jsonArray=new JSONArray();
        for (int i = 0; i < list.size(); i++) {

            SiteReservation siteReservation1 = list.get(i);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("userId", siteReservation1.getUserId());

            //String userName = siteUserTestMapper.findNameById(siteReservation1.getUserId());
            String userName = siteUserTestService.findNameById(siteReservation1.getUserId());

            jsonObject.put("userName",userName);
            jsonObject.put("siteYyId",siteReservation1.getSiteYyId());
            jsonObject.put("applicationTime", siteReservation1.getApplicationTime());
            jsonObject.put("whenTime", siteReservation1.getWhenTime());
            jsonObject.put("status", siteReservation1.getStatus());
            jsonObject.put("reason", siteReservation1.getReason());
            Integer siteId = siteReservation1.getSiteId();
            jsonObject.put("siteId",siteId);
            SiteInformation siteInformationBySiteId = siteInformationService.findSiteInformationBySiteId(siteId);
            jsonObject.put("name", siteInformationBySiteId.getName());
            jsonObject.put("position",siteInformationBySiteId.getPosition());
            jsonObject.put("price", siteInformationBySiteId.getPrice());
            jsonArray.add(jsonObject);

        }
        JSONObject jsonObject = new JSONObject();
        long total = allSiteReservation.getTotal();
        jsonObject.put("total", total);
        jsonObject.put("rows",jsonArray);

        return jsonObject;
    }


    @RequestMapping("/modifyShenqingZhuangtai")
    public Map<String,Object> modifyShenqingZhuangtai(SiteReservation siteReservation){
        int i = siteReservationService.modifyShenqingZhuangtai(siteReservation);
        Map<String,Object> map=new HashMap<>();
        if (i>0){
            map.put("success",true);
        }else {
            map.put("success",false);
        }
        return map;
    }

}
